﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
////using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DBSourceToolsLib.UI.Forms
{
    public partial class ProgressForm : Form
    {
        private Thread m_Thread;
        delegate void CloseCallback(string dummy);
        delegate void SetTextCallback(string text);
        delegate void SetProgressCallback(string percent);
        public string ErrorMessage;

        public ProgressForm()
        {
            InitializeComponent();
        }

        Interfaces.IProgressEvent oProgressEventSource;

        public void setEventSource( Interfaces.IProgressEvent oProgressObject)
        {
            this.oProgressEventSource = oProgressObject;
            oProgressEventSource.ProgressEventPublisher += this.EventReceived;
//            
        }
        private void EventReceived(object source, DBSourceToolsLib.ProgressEventArgs args)
        {
            if (args.Percent.Contains("-"))
            {
                // an error occurred
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(args.Message);
                oError.Show();

                //System.Windows.Forms.MessageBox.Show(args.Message);
            }
            else
            {
                this.SetText(args.Message);
                this.SetProgress(args.Percent);
            }

        }

        private void ProgressForm_Load(object sender, EventArgs e)
        {
            m_Thread = new Thread(new ThreadStart(this.ThreadFunc));
            m_Thread.Start();
            this.m_lblText.Text = "Initialising ..";
        }

        private void ThreadFunc()
        {
            try
            {
                oProgressEventSource.Execute();
            }
            catch (Exception ex)
            {
                oProgressEventSource.RegisterError(ex.ToString());
            }
            this.CloseDialog("dummy");
        }

        private void CloseDialog(string dummy)
        {
            if (this.InvokeRequired)
            {
                CloseCallback d = new CloseCallback(CloseDialog);
                this.Invoke(d, new object[] { dummy });
            }
            else
            {
                this.Close();
            }
        }

        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.m_lblText.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.m_lblText.Text = text;
                this.m_lblText.Update();
            }
        }

        private void SetProgress(string percent)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.m_progressBar.InvokeRequired)
            {
                SetProgressCallback d = new SetProgressCallback(SetProgress);
                this.Invoke(d, new object[] { percent });
            }
            else
            {
                this.m_progressBar.Value = Convert.ToInt16(percent);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            // cleanup thread when closing dialog.
            m_Thread.Abort();
            m_Thread.Join();
            base.OnClosing(e);

        }
    }
}
